package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.MessageDao;
import cn.sumpu.app.guoguojie.domain.Message;
import cn.sumpu.app.guoguojie.domain.Ext.MessageExt;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class MessageDaoImpl extends SqlSessionDaoSupport implements MessageDao {

	private final static Logger logger = LoggerFactory
			.getLogger(MessageDaoImpl.class);

	

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getMessageList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageList failure.", e);
			}
			throw new AppBusinessException("getMessageList failure.", e);
		}
	}

	@Override
	public int getMessageListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageListCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageListCount failure.", e);
			}
			throw new AppBusinessException("getMessageListCount failure.", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MessageExt> getMessageFriend(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageFriend",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageFriend failure.", e);
			}
			throw new AppBusinessException("getMessageFriend failure.", e);
		}
	}

	@Override
	public int getMessageFriendCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageFriendCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageFriendCount failure.", e);
			}
			throw new AppBusinessException("getMessageFriendCount failure.", e);
		}
	}
	
	
	

	@SuppressWarnings("unchecked")
	@Override
	public List<MessageExt> getMessageInner(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageInner",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageFriend failure.", e);
			}
			throw new AppBusinessException("getMessageFriend failure.", e);
		}
	}

	@Override
	public int getMessageInnerCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageInnerCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageInnerCount failure.", e);
			}
			throw new AppBusinessException("getMessageInnerCount failure.", e);
		}
	}

	@Override
	public void insertMessage(Message message) throws AppBusinessException {
		try {
			getSqlSession().insert("cn.sumpu.app.guoguojie.persistence.MessageMapper.insertMessage",message);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertMessage failure.", e);
			}
			throw new AppBusinessException("insertMessage failure.", e);
		}
	}

	@Override
	public void deleteMessageById(String identifier)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessageById",identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMessageById failure.", e);
			}
			throw new AppBusinessException("deleteMessageById failure.", e);
		}
	}

	@Override
	public void deleteMessageByFamily(String family)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessageByFamily",family);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMessageByMember failure.", e);
			}
			throw new AppBusinessException("deleteMessageByMember failure.", e);
		}
	}

	@Override
	public int getMessageNoReadCount(Map<String,Object> map) throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne("cn.sumpu.app.guoguojie.persistence.MessageMapper.getMessageNoReadCount",map);
			return (Integer) (obj==null?0:obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getMessageFriendCount failure.", e);
			}
			throw new AppBusinessException("getMessageFriendCount failure.", e);
		}
	}

	@Override
	public void updateMessage(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession().update("cn.sumpu.app.guoguojie.persistence.MessageMapper.updateMessage",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateMessage failure.", e);
			}
			throw new AppBusinessException("updateMessage failure.", e);
		}
	}

	@Override
	public void deleteMessageByReplyId(String messageByReplyId)
			throws AppBusinessException {
		try {
			getSqlSession().delete("cn.sumpu.app.guoguojie.persistence.MessageMapper.deleteMessageByReplyId",messageByReplyId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteMessageByReplyId failure.", e);
			}
			throw new AppBusinessException("deleteMessageByReplyId failure.", e);
		}
	}

	
	

}
